package com.代理模式;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class JDKDaili {
    public static void main(String[] args) {
        //本地必须实现接口
        LawyerProxy lawyerProxy = new LawyerProxy(new ZhangSan());
        Speaker speaker = (Speaker) Proxy.newProxyInstance(JDKDaili.class.getClassLoader(), new Class[]{Speaker.class},
                lawyerProxy); //获得具体的代理对象
        speaker.speak();
    }
}

class LawyerProxy implements InvocationHandler{
    Lock lock = new ReentrantLock();

    private Object obj;

    public LawyerProxy(Object obj){
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if(method.getName().equals("speak")){
            System.out.println("引用法律条文！");
            method.invoke(obj, args); //反射
            System.out.println("打人是不对的！");
        }
        return null;
    }
}
